// pages/carInfodetail/carInfodetail.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    toView: '',
    scrollTop: 100,
    carArray: [],
    carArrayAll:[],
    letterArray: ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"],
    formData:''
  },
  indexEvent: function (e) {
    this.setData({
      toView: e.currentTarget.id
    })
  },
  bingfocusFunc: function (e) {
    this.setData({
      focus: true,
      focusclass: 'focus'
    })
  },
  bingblurFunc: function (e) {
    this.setData({
      focusclass: ''
    })
  },
  bindtapcar: function (e) {
    var item = e.currentTarget.dataset.item;
    var that = this;
    this.data.formData.brandCar = item.id;
    this.data.formData.brandCarName = item.brandCarName;
    wx.setStorage({
      key: 'personalInfo',
      data: this.data.formData,
    })
    wx.navigateBack({
      delta:1
    })
    // wx.navigateTo({
    //   url: '../../pages/carInfo/carInfo?brandCar=' + item.id + '&brandCarName='+item.brandName
    // });

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var param={};
    var _this = this;
    wx.getStorage({
      key: 'personalInfo',
      success: function (res) {
        _this.setData({
          formData: res.data
        })
      }
    })
    wx.request({
      url: app.globalData.httpApiCar + 'tGyCarBrand/all', //仅为示例，并非真实的接口地址
      data: param,
      success: function (res) {
        // var arr = res.data.data.reverse();
        var arr = [];
        var obj='';
        _this.data.letterArray.forEach(function(item){
          var flag=false;
          var tArr=[];
          res.data.data.forEach(function (ele, index) {
            if (ele.abbreviations==item){
              flag=true;
              tArr.push(ele);
            }
          })
          if(flag){
            if (obj == '') {
              obj = item;
            } else {
              obj += ',' + item;
            }
            arr.push({ type: 1, abbreviations: item });
            arr = arr.concat(tArr);
          }
        })
        _this.setData({
          carArray: arr,
          carArrayAll:arr,
          letterArray:obj.split(',')
        })
      }
    })
  },
  seachFun:function(e){
    var arr=[];
    var obj='';
    this.data.carArrayAll.forEach(function(ele){
      if (ele.brandName){
        if (ele.brandName.indexOf(e.detail.value)>=0){
          if (obj.indexOf(ele.abbreviations) < 0) {
            if (obj == '') {
              obj = ele.abbreviations;
            } else {
              obj += ',' + ele.abbreviations;
            }
            arr.push({ type: 1, abbreviations: ele.abbreviations })
          }
          arr.push(ele);
        }
      }
    })
    this.setData({
      carArray: arr,
      letterArray: obj.split(',')
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})